<?php

namespace app\speedDating\services;

use app\speedDating\exception\ServiceRuntimeException;
use app\speedDating\interfaces\KeyName;
use app\speedDating\interfaces\ServiceCode;
use app\speedDating\interfaces\ServiceMsgText;
use app\speedDating\model\SddUser;
use app\speedDating\services\common\JWTAuthUserService;
use app\speedDating\services\common\JWTService;
use app\speedDating\services\common\UtilsService;
use think\db\exception\DbException;
use think\facade\Cache;
use think\facade\Db;

class MemberService
{

    /**
     * 创建用户并返回token
     * @param $data
     * @return array
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function register($data)
    {
        //手机号检查
        $user = SddUser::getInfoByUsername($data['username']);
        if (!empty($user)) {
            throw new ServiceRuntimeException(ServiceMsgText::ERR_VALIDATE_MEMBER_EXIST,ServiceCode::ERR_VALIDATE_MEMBER_EXIST);
        }

        $data['password'] = UtilsService::bcryptHash($data['password_confirmation']);
        $data['nickname'] = UtilsService::compileStr($data['username']);
        $data['inviter_id'] = $data['inviter_id'] ?? 0;

        $user = Db::transaction(function () use ($data) {
            $user = SddUser::createUser($data);
            $insDataAccount['user_id'] = $user->id;
            //资金账户
            (new FinanceService())->createAccount($insDataAccount);
            return $user;
        });

        if (!empty($user)) {
            //新会员统计缓存
            $uniqueKey = KeyName::COUNT_CACHE_NEW_MEMBER;
            Cache::store(KeyName::CACHE_DRIVER)->inc($uniqueKey);

            return JWTService::authorizations($user);
        }
        throw new ServiceRuntimeException(ServiceMsgText::ERR_SERVER_ERROR_EXECUTE,ServiceCode::ERR_SERVER_ERROR_EXECUTE);
    }

    /**
     * 会员登录返回token
     * @param $data
     * @return array
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function login($data)
    {
        $user = SddUser::getInfoByUsername($data['username']);
        if (empty($user)) {
            throw new ServiceRuntimeException(ServiceMsgText::ERR_VALIDATE_MEMBER_NOT_EXIST,ServiceCode::ERR_VALIDATE_MEMBER_NOT_EXIST);
        }

        if (UtilsService::bcryptVerify($data['password'], $user->password)) {
            return JWTService::authorizations($user);
        }
        throw new ServiceRuntimeException(ServiceMsgText::ERR_UNAUTHORIZED_PASSWORD_ERROR,ServiceCode::ERR_UNAUTHORIZED_PASSWORD_ERROR);
    }

    /**
     * 返回当前登录用户详情
     * @return mixed
     * @throws DbException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail()
    {
        return JWTAuthUserService::user();
    }


    /**
     * 用户修改数据
     * @param $data
     * @param $userID
     * @return void
     */
    public function update($data, $userID)
    {
        SddUser::toUpdate($data, ['id' => $userID]);
    }

}